package com.hxci.web.controller;

import com.hxci.web.bean.Conn;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


import static java.lang.System.out;
//增删改查样例
public class  UserServlet extends HttpServlet {
    ServletConfig servletConfig;
    @Override
    public void init(ServletConfig config) throws ServletException {
        servletConfig=config;

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String actionType = req.getParameter("actionType");
        switch (actionType) {
            case "user_update":
                do_update(req, resp);
                break;
            case "user_add":
                do_add(req, resp);
                break;
            default:
                out.println("usererror:"+actionType);
                break;
        }
    }

    private void do_update(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        ServletContext ctx = servletConfig.getServletContext();
        String realPath = ctx.getRealPath("/WEB-INF/db.properties");
        String userID = req.getParameter("user_id");
        String Login_account = req.getParameter("login_account");
        String Login_pwd = req.getParameter("login_pwd");
        String email = req.getParameter("email");
//        String[] Hobby = req.getParameterValues("hobby");
//        String Remark = req.getParameter("remark");


        String sql = "UPDATE user SET login_account = ?, login_pwd = ?, email = ? WHERE user_id = ?";
        List<Object> params = new ArrayList<>();
        params.add(Login_account);
        params.add(Login_pwd);
        params.add(email);
//        params.add(String.join(",", Hobby));
//        params.add(Remark);
        params.add(userID);
        Conn c = new Conn(realPath);
        c.doQuery(sql, params);
        if (req.getSession().getAttribute("login_account").equals("user")) {
            resp.sendRedirect("user.jsp");
        } else {
            resp.sendRedirect("accout.jsp");
        }
    }

    protected void do_add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        ServletContext ctx=servletConfig.getServletContext();
        String realPath=ctx.getRealPath("/WEB-INF/db.properties");

        String login_account = req.getParameter("username");
        String login_pwd = req.getParameter("password");
        String email = req.getParameter("email");
        out.println(login_account+":"+login_pwd);
        String sql = "INSERT INTO user (login_account, login_pwd, email )" +
                "VALUES" +
                "(?,?, ?);";
            List<Object> params = new ArrayList<>();
            params.add(login_account);
            params.add(login_pwd);
            params.add(email);
            Conn c = new Conn(realPath);
            c.doQuery(sql, params);
            //在页面上显示输出成功
            PrintWriter out = resp.getWriter();
        resp.sendRedirect("login.jsp");

        }




}
